package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

import java.util.ArrayList;
import java.util.Stack;

/**
 * @author jinhu
 * created 2021-05-08 16:34
 */
public class Solution_0101 {
    public ArrayList<Integer> pre = new ArrayList<>();
    public Stack<Integer> pos = new Stack<>();

    /**
     * 镜像对称二叉树
     * 前序遍历和后序遍历的结果是相反的
     * 1
     * 2     2
     * 3    4 4    3
     * 完全对称，要求左右子树都相等
     * 1
     * 2   2
     * 3 3 4 4
     *
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        this.pre(root);
        this.pos(root);
        for (Integer temp : pre
        ) {
            if (temp != pos.pop()) {
                return false;
            }
        }
        return true;

    }

    public void pre(TreeNode treeNode) {
        if (treeNode == null) {

            return;
        }
        pre.add(treeNode.val);
        pre(treeNode.left);
        pre(treeNode.right);
    }

    public void pos(TreeNode treeNode) {
        if (treeNode == null) {

            return;
        }
        pos(treeNode.left);
        pos(treeNode.right);
        pos.add(treeNode.val);
    }
}
